import Qt 4.7

Rectangle {
    id: mainContainer
    width: 514; height: 184
    color: "transparent"

    property alias model: listView.model

    signal callEditMoney
    signal callMessageBox(string mesgBox, string nextView)
    signal callUpdateMoneyRegisters;

    function updateMoneyRegisters(m_model) {
        console.debug("QML:CashFlowList - Updating model");
        listView.model = m_model;
        totalRectangle.updateTotal();
        dateSelector.updatePeriods();
    }

    Rectangle {
        id: listRectangle

        x: 0; y: 40;
        width: 514; height: 158
        color: "transparent"

        ListView {
            id: listView
            anchors.fill: parent
            clip: true
            delegate: listMoneyDelegate
            interactive: !popupButton.visible &&
                         (dateSelector.dropDownState == 'normal' && cashFlowMode.state == 'normal')
            highlight: Rectangle { color: Qt.lighter("gray"); opacity: 0.5; radius: 1 }
            highlightFollowsCurrentItem: true
            onModelChanged: {
                console.debug("Model has changed");
            }
        }
        Component {
            id: listMoneyDelegate

            Rectangle {
                id: wrapper

                x: -10
                height: 40; width: 600
                color: "transparent"
                border { width: 1; color: Qt.lighter("gray")}

                Column {
                    x: 55; y: 14

                    Text {
                        font.pointSize: 6;
                        font.weight: Font.DemiBold;
                        font.family: "Series 60 Sans"
                        text: model.modelData.description;
                    }
                }
                Column {
                    x: 260; y: 14

                    Text {
                        font.pointSize: 6;
                        font.weight: Font.DemiBold;
                        font.family: "Series 60 Sans"
                        text: model.modelData.category.name;
                    }
                }
                Column {
                    x: 398; y: 14

                    Text {
                        font.pointSize: 6;
                        font.weight: Font.DemiBold;
                        font.family: "Series 60 Sans"
                        text: (model.modelData.moneyType == 0? '+' : '-') + " R$ " + model.modelData.amount;
                    }
                }

                MouseArea {
                    enabled: (!model.modelData.editMode) &&
                             (dateSelector.dropDownState == 'normal' && cashFlowMode.state == 'normal')
                    anchors.fill: wrapper
                    onPositionChanged: {
                        if (popupButton.visible && popupButton.itemNumber != index) {
                            popupButton.visible = false;
                        }
                    }
                    onClicked: {
                        if (popupButton.visible ) {
                            popupButton.visible = false;
                        }
                        console.debug(ListView.view.indexAt(0,wrapper.y));
                        // Updating the current index to ensure the correct highlight
                        ListView.view.currentIndex = ListView.view.indexAt(100, wrapper.y);
                    }
                    onPressAndHold: {
                        // Updating the current index to ensure the correct highlight
                        ListView.view.currentIndex = ListView.view.indexAt(100, wrapper.y);
                        // Find out the correct coordinates of the popup button
                        var newCoordinates = listRectangle.mapFromItem(wrapper, mouse.x, mouse.y);
                        console.debug(mouse.y);
                        console.debug(newCoordinates.y);
                        popupButton.visible = false;
                        popupButton.itemNumber = ListView.view.currentIndex;
                        if (newCoordinates.y + 50 > listRectangle.height)
                            popupButton.y = newCoordinates.y - 40;
                        else popupButton.y = newCoordinates.y + 10;
                        popupButton.visible = true;
                    }
                }
            }
        }
        PopupButton {
            id: popupButton
            visible: false
            x: 156;
            onEditClicked: {
                visible = false;
                daoManager.setMoney2BeEdited(listView.currentIndex);
                mainContainer.callEditMoney();
            }
            onRemoveClicked: {
                listView.model = daoManager.removeMoneyRegister(listView.currentIndex);
                visible = false;
            }
        }
    }
    Rectangle {
        id: totalRectangle

        x: 0; y: 199
        width: 514; height: 44
        color: "#333132"


        function updateTotal() {
            console.debug("Updating totals by category");
            totalIncomes.text = "+ R$ " + daoManager.totalIncomes();
            totalOutComes.text = "- R$ " + daoManager.totalOutcomes();
            totalCashFlow.text = "R$ " + daoManager.totalCashFlow();
        }

        Text {
            id: totalIncomes

            x: 50;  y: 10
            color: "white"
            text: "+ R$ " + daoManager.totalIncomes()
        }
        Text {
            id: totalOutComes

            x: 215; y: 10
            color: "white"
            text: "- R$ " + daoManager.totalOutcomes()
        }
        Text {
            id: totalCashFlow

            x: 392; y: 10
            color: "white"
            text: "R$ " + daoManager.totalCashFlow()
        }
    }
    MouseArea {
        anchors.fill: parent
        enabled: dateSelector.dropDownState == 'extended' || cashFlowMode.state == 'extended'
        onClicked: {
            if (dateSelector.dropDownState == 'extended')
                dateSelector.dropDownState = 'normal'
            if (cashFlowMode.state == 'extended')
                cashFlowMode.state = 'normal'
        }
    }
    AdvancedDropDown {
        id: cashFlowMode

        x: 386; y: -10
        rowPosX: 0;
        rowTextX: -10
        color: "transparent"
        model: daoManager.getAllTypeMoney()
        allowCreate: false
        width: 120; height: 40
        internalWidth: 120; normalWidth: 120; extendedWidth: 130
        onItemSelected: {
            console.debug("QML:CashFlowList - GETTING " + textSelected);
            daoManager.setMoneyType(idSelected - 1);
            mainContainer.callUpdateMoneyRegisters();
            mainContainer.callMessageBox("Atualizando Dados", "CashFlowView");
            console.debug("QML:CashFlowList - GETTING " + textSelected);
        }

        textSelected: "Todos"
    }
    DateSelector {
        id: dateSelector

        x: 42; y: 0

        onChangedDate: mainContainer.callUpdateMoneyRegisters();
    }
}
